package com.sany.taskmanager.http;

import android.text.TextUtils;

import com.sany.taskmanager.global.Key;
import com.sany.taskmanager.util.PreferenceUtil;

import java.util.Objects;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiClient {

    /**
     * 开发环境
     */
    public static final String URL_DEV = "http://10.16.196.122:8080/boom/appServer/";
    /**
     * 正式环境
     */
    public static final String URL_PRO = "http://10.16.5.33:8082/boom/appServer/";

    private static volatile ApiClient mInstance;
    private final Retrofit retrofit;
    private static String BASE_URL = URL_DEV;
    private ApiService apiService;

    private ApiClient() {
        String ip = PreferenceUtil.getPreferences().getString(Key.SharedKey.IP);
        if (!BASE_URL.contains("10.16.5.33") && !TextUtils.isEmpty(ip)) {
            BASE_URL = "http://".concat(ip).concat(":8080/boom/appServer/");
        }
        retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
                .client(Objects.requireNonNull(UnsafeOkHttpClient.getUnsafeOkHttpClient()))
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build();
    }

    public static ApiClient getInstance() {
        if (mInstance == null) {
            synchronized (ApiClient.class) {
                if (mInstance == null) {
                    mInstance = new ApiClient();
                }
            }
        }
        return mInstance;
    }


    public ApiService getApiService() {
        if (apiService == null) {
            apiService = retrofit.create(ApiService.class);
        }
        return apiService;
    }

}
